package ar.Location.Weather_2;

import java.io.InputStream;

import javax.xml.parsers.SAXParserFactory;

import android.util.Xml;
import ar.Location.Constant.Define;
import ar.Location.Helper.HttpProvider;
import ar.activity.R.drawable;

public class LocalWeatherProvider {
	public static String urlBaseRequest = "http://api.wunderground.com/api/@api_key/conditions/forecast/q/@latitude,@longitude.xml";

	public static LocalWeather getWeatherForAddress(String latitude,
			String longitude) {
		if (latitude != null && longitude != null) {
			if (latitude.length() > 0 && longitude.length() > 0) {
				try {
					String urlRequest = urlBaseRequest.replace("@api_key",
							Define.WUNDERGROUND_API_KEY);
					urlRequest = urlRequest.replace("@latitude", latitude);
					urlRequest = urlRequest.replace("@longitude", longitude);
					SAXParserFactory spf = SAXParserFactory.newInstance();
					spf.setValidating(false);
					InputStream inputStream = HttpProvider
							.getInputStream(urlRequest);
					LocalWeatherHandler weatherHandler = new LocalWeatherHandler();
					Xml.parse(inputStream, Xml.Encoding.UTF_8, weatherHandler);
					return weatherHandler.getLocalWeather();
				} catch (Exception e) {
					// TODO: handle exception
					return null;
				}
			}
		}
		return null;
	}

	public static int getResId(String weather) {
		if (weather.toLowerCase().equals("cloudy")) {
			return drawable.cloudy;
		}
		if (weather.toLowerCase().equals("chance of rain")) {
			return drawable.chance_of_rain;
		}
		if (weather.toLowerCase().equals("chance of snow")) {
			return drawable.chance_of_snow;
		}
		if (weather.toLowerCase().contains("chance")
				&& weather.toLowerCase().contains("storm")) {
			return drawable.chance_of_storm;
		}
		if (weather.toLowerCase().equals("dust")) {
			return drawable.dust;
		}
		if (weather.toLowerCase().equals("fog")) {
			return drawable.fog;
		}
		if (weather.toLowerCase().equals("haze")) {
			return drawable.haze;
		}
		if (weather.toLowerCase().equals("icy")) {
			return drawable.icy;
		}
		if (weather.toLowerCase().equals("mist")) {
			return drawable.mist;
		}
		if (weather.toLowerCase().equals("mostly cloudy")) {
			return drawable.mostly_cloudy;
		}
		if (weather.toLowerCase().equals("mostly sunny")) {
			return drawable.mostly_sunny;
		}
		if (weather.toLowerCase().contains("scattered")
				&& weather.toLowerCase().contains("cloud")) {
			return drawable.partly_cloudy;
		}
		if (weather.toLowerCase().equals("partly cloudy")) {
			return drawable.partly_cloudy;
		}
		if (weather.toLowerCase().equals("partly sunny")) {
			return drawable.partly_sunny;
		}
		if (weather.toLowerCase().equals("rain")) {
			return drawable.rain;
		}
		if (weather.toLowerCase().equals("sleet")) {
			return drawable.sleet;
		}
		if (weather.toLowerCase().equals("smoke")) {
			return drawable.smoke;
		}
		if (weather.toLowerCase().equals("snow")) {
			return drawable.snow;
		}
		if (weather.toLowerCase().equals("storm")) {
			return drawable.storm;
		}
		if (weather.toLowerCase().equals("thunderstorm")) {
			return drawable.storm;
		}
		if (weather.toLowerCase().equals("sunny")
				|| weather.toLowerCase().equals("sun")) {
			return drawable.sunny;
		}
		if (weather.toLowerCase().contains("showers")) {
			return drawable.showers;
		}
		if (weather.toLowerCase().equals("overcast")) {
			return drawable.overcast;
		}
		if (weather.toLowerCase().equals("clear")) {
			return drawable.clear;
		}
		return -1;
	}
}
